<?php
    include_once './config.php';
    $user = checkLogin();
    include_once './config.php';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $content = $_POST['content'];
        $money = $_POST['money'];
        $imgs = $_POST['imgs'];
        $type = $_POST['type'];
        $query = "insert into whw_supplement (user_id,content,money,imgs,type,createtime,updatetime) values (".getInt($user['id']).",".quote($content).",".getFloat($money).",".quote($imgs).",".getInt($type).",".time().",".time().")";
        $objDBCD14->execute($query);
        $supplement_id = $objDBCD14->lastInsertedId();
        if($content){
            $query = "insert into whw_supplement_log (user_id,supplement_id,name,content,createtime) values (".getInt($user['id']).",".getInt($supplement_id).",'content',".quote($content).",".time().")";
            $objDBCD14->execute($query);
        }
        if($money){
            $query = "insert into whw_supplement_log (user_id,supplement_id,name,content,createtime) values (".getInt($user['id']).",".getInt($supplement_id).",'money',".getFloat($money).",".time().")";
            $objDBCD14->execute($query);
        }
        if($imgs){
            $query = "insert into whw_supplement_log (user_id,supplement_id,name,content,createtime) values (".getInt($user['id']).",".getInt($supplement_id).",'imgs',".quote($imgs).",".time().")";
            $objDBCD14->execute($query);
        }
        $msg = "添加成功";
        echo json_encode(['code'=>$error ? 0 : 1,'msg'=>$msg]);
        exit;
    }
    $type = $_GET['type'];
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>补充</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        .container{
            margin: 0;
            padding: 20px 10px;
        }
        .layui-form-label{
            float: unset;
            text-align: left;
            padding: 0;
        }
        .layui-input-block{
            float: unset;
            margin-left: 0;
            margin-top: 10px;
        }
        .images{
            display: flex;
            align-items: center;
            flex-wrap: wrap;
        }
        .images .item{
            width: 33.33%;
            min-width: 33.33%;
            max-width: 33.33%;
            position: relative;
            margin-top: 5px;
        }
        .images .item img{
            width: calc(33.33vw - 10px);
            height: calc(33.33vw - 10px);
            border-radius: 5px;
        }
        .images .item i{
            position: absolute;
            top: 5px;
            right: 5px;
        }
    </style>
</head>
<body ontouchstart>
    <div class="container" id="container">
        <form class="layui-form" action="" id="myForm">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">金额</label>
                <div class="layui-input-block">
                   <input type="text" name="money" required lay-verify="required" placeholder="请输入金额" autocomplete="off" class="layui-input">    
                </div>
            </div>  
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">说明</label>
                <div class="layui-input-block">
                  <textarea name="content" placeholder="请输入说明" class="layui-textarea"></textarea>
                </div>
            </div>  
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">图片</label>
                <div class="images">
                   
                </div>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="upload">
                      <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
            </div>  
            <input type="hidden" name="type" value="<?=$type?>"/>
            <button type="submit" class="layui-btn layui-btn-danger layui-btn-lg"
          style="width: 100%;margin-top: 5px" id="submitForm">提交</button>
        </form>
    </div>
    <!-- 引入 layui.js -->
    <script src="./layui/layui.js"></script>
    <script>
        layui.use(['jquery','layer', 'form','code','upload'], function(){
            var $ = layui.$,layer = layui.layer,form = layui.form,code = layui.code,upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                ,url: 'upload.php' //上传接口
                ,accept: "images"
                ,multiple: true
                ,done: function(res){
                  if(res.code == 1){
                    $(".images").append('<div class="item"><img class="layui-img" src="'+res.data.src+'"/><i class="layui-icon layui-icon-close-fill del-img"></i></div>')   
                  }
                  //上传完毕回调
                }
                ,error: function(){
                  //请求异常回调
                }
            });
            $(document).on('click', '.del-img', function() {
                $(this).parent().remove()
            })
            // $(document).on('click', '.layui-img', function() {
            //     layer.open({
            //       type: 1,
            //       title: false,
            //       closeBtn: 0,
            //       area: 'auto',
            //       skin: 'layui-layer-nobg', // 没有背景色的class
            //       shadeClose: true,
            //       content: "<img src='" + this.src + "' style='max-width:100%'>"
            //     });
            // })
            $("#myForm").submit(function(){
                var formValues = $("#myForm").serializeArray();
                var param = {}
                for(var i = 0;i < formValues.length;i++){
                    param[formValues[i].name] = formValues[i].value
                }
                // 获取图片
                var imgs = []
                $('.layui-img').each(function(){
                    imgs.push($(this).attr("src"))
                })
                param.imgs = imgs.join(",") 
                $.post("supplement-add.php",param,function(res){
                    res = JSON.parse(res)
                    layer.msg(res.msg);
                    if(res.code == 1){
                        var index = parent.layer.getFrameIndex(window.name); 
                        setTimeout(function(){
                            parent.layer.close(index);
                            parent.location.reload()
                        },1000)
                    }
                })
                return false
            })
        });
    </script>  
</body>
</html>